package com.tanhua.server.controller;

import com.tanhua.commons.utils.JwtUtils;
import com.tanhua.model.db.UserInfo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.UserService;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 保存用户信息
     * */
    @PostMapping("/loginReginfo")
    public ResponseEntity loginReginfo(@RequestBody UserInfo userInfo, @RequestHeader("Authorization") String token){
//        //判断token
//        boolean verifyToken = JwtUtils.verifyToken(token);
//        if (!verifyToken){
//            throw  new RuntimeException("用户未登入");
//        }
//        //解析token
//        Claims claims = JwtUtils.getClaims(token);
//        Long id = claims.get("id", Long.class);
        Long userId = UserHolder.getUserId();
        //设置id
        userInfo.setId(userId);
        //保存用户信息
        userService.saveUserInfo(userInfo);
        return ResponseEntity.ok(null);
    }
    /**
     * 头像更新
     * */
    @PostMapping("loginReginfo/head")
    public ResponseEntity head(MultipartFile headPhoto, @RequestHeader("Authorization") String token) throws IOException {
//        //判断token
//        boolean verifyToken = JwtUtils.verifyToken(token);
//        if (!verifyToken){
//            throw  new RuntimeException("用户未登入");
//        }
//        //解析token
//        Claims claims = JwtUtils.getClaims(token);
//        Long id = claims.get("id", Long.class);
        Long id = UserHolder.getUserId();
        //设置头像
        userService.setHead(id,headPhoto);
        return ResponseEntity.ok(null);
    }


}
